package com.jn.glide.views;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import com.jn.glide.R;
import com.jn.glide.entity.GroupEntity;
import com.jn.glide.entity.PictureEntity;
import com.jn.glide.entity.ResultEnity;
import com.jn.glide.service.ApiService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ListActivity extends AppCompatActivity {
    private ListView listView;
    private ProgressBar progressBar;
    private static final String TAG = "ListActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        listView = findViewById(R.id.listView);
        progressBar = findViewById(R.id.progressBar);

        String url = getIntent().getStringExtra("URL");
        if (url != null && !url.isEmpty()) {
            fetchPictures(url);
        } else {
            Log.e(TAG, "URL is null or empty");
            // 处理没有URL的情况
        }
    }

    private void fetchPictures(String url) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.oioweb.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService apiService = retrofit.create(ApiService.class);
        Call<ResultEnity> call = apiService.getPictures(url);

        progressBar.setVisibility(View.VISIBLE);
        call.enqueue(new Callback<ResultEnity>() {
            @Override
            public void onResponse(Call<ResultEnity> call, Response<ResultEnity> response) {
                progressBar.setVisibility(View.GONE);
                if (response.isSuccessful() && response.body() != null) {
                    List<GroupEntity> groups = response.body().getResult();
                    if (groups != null && !groups.isEmpty()) {
                        ArrayAdapter<PictureEntity> adapter = new ArrayAdapter<>(ListActivity.this, android.R.layout.simple_list_item_1, groups.get(0).getList());
                        listView.setAdapter(adapter);
                    }
                } else {
                    Log.e(TAG, "Response is not successful or body is null");
                }
            }

            @Override
            public void onFailure(Call<ResultEnity> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                Log.e(TAG, "Failed to fetch pictures", t);
            }
        });

        listView.setOnItemClickListener((parent, view, position, id) -> {
            GroupEntity group = (GroupEntity) listView.getAdapter().getItem(position);
            if (group != null && group.getList() != null && !group.getList().isEmpty()) {
                PictureEntity picture = group.getList().get(0); // 这里你可能需要更改获取图片的逻辑
                Intent intent = new Intent(ListActivity.this, ImageDisplayActivity.class);
                intent.putExtra("IMAGE_URL", picture.getIcon()); // 确保传递有效的图片 URL
                startActivity(intent); // 启动 ImageDisplayActivity
            } else {
                Log.e(TAG, "Clicked item or its picture list is null");
            }
        });
    }
}
